
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using DecisionMaking.WebApp;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Core.Resource;
using Castle.ActiveRecord.Framework.Config;
using System.Reflection;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using Castle.MicroKernel.Registration;

namespace WebApp
{
	public class MvcApplication : System.Web.HttpApplication
	{
		public static void RegisterRoutes (RouteCollection routes)
		{
			routes.IgnoreRoute ("{resource}.axd/{*pathInfo}");
			
			routes.MapRoute ("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" });
			
		}

		protected void Application_Start ()
		{
			RegisterRoutes (RouteTable.Routes);
			
			// initialize log4net
			log4net.Config.XmlConfigurator.Configure();
						
			// initialize Windsor
 			WindsorContainer container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));			
			
			// Initialize ActiveRecord
			ActiveRecordStarter.UpdateSchema();			
			
			// Register Windsor components
		    container.Register(
			    // All services from the BusinessLogic assembly
			    AllTypes
			         .FromAssemblyNamed("BusinessLogic")
			         .Where(Component.IsInNamespace("DecisionMaking.BusinessLogic.Impl"))
			         .WithService.FirstInterface(),			         
			    			               
				// The WindsorControllerFactory	               
				Component
                    .For<IControllerFactory>()
                    .ImplementedBy<WindsorControllerFactory>(),
			                   
			    // All MVC Controllers               
		      	AllTypes
		        	.FromAssembly(Assembly.GetExecutingAssembly())
		        	.BasedOn<IController>()
			    	.Configure(reg => reg.Named(reg.Implementation.Name.ToLowerInvariant()).LifeStyle.Transient)
		    );
			
			// initialize Controller Factory
			ControllerBuilder.Current.SetControllerFactory(container.Resolve<IControllerFactory>());    
		}
		
		
	}
}

